/* ****************************************************************** **
**    OpenSees - Open System for Earthquake Engineering Simulation    **
**          Pacific Earthquake Engineering Research Center            **
**                                                                    **
**                                                                    **
** (C) Copyright 1999, The Regents of the University of California    **
** All Rights Reserved.                                               **
**                                                                    **
** Commercial use of this program without express permission of the   **
** University of California, Berkeley, is strictly prohibited.  See   **
** file 'COPYRIGHT'  in main directory for information on usage and   **
** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
**                                                                    **
** Developed by:                                                      **
**   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
**   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
**   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
**                                                                    **
** ****************************************************************** */

// Written: User-defined implementation for UDP_LoadPattern
// Purpose: This file contains the class definition for UDP_LoadPattern.

#ifndef UDP_LoadPattern_h
#define UDP_LoadPattern_h

#include <LoadPattern.h>
#include <Vector.h>
#include <ID.h>
#include <Node.h>

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
typedef int ssize_t;
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif

class UDP_LoadPattern : public LoadPattern
{
public:
	UDP_LoadPattern(int tag, int nodeTag, int dof, int port, bool waitForData = true);
	~UDP_LoadPattern();

	void applyLoad(double time);
	void setDomain(Domain* theDomain);
	int sendSelf(int commitTag, Channel& theChannel);
	int recvSelf(int commitTag, Channel& theChannel, FEM_ObjectBroker& theBroker);
	void Print(OPS_Stream& s, int flag = 0);

private:
	int theNodeTag;
	int theDOF;
	int serverPort;
	bool waitForData;

	Node* theNode;
	Vector theLoad;

#ifdef _WIN32
	SOCKET sockfd;
	WSADATA wsaData;
#else
	int sockfd;
#endif

	struct sockaddr_in servaddr;
	bool socketInitialized;

	int initializeSocket();
	double receiveData();
	void cleanupSocket();
};

#endif